package fule.com.playandroidkotlin.ui.fragment

import android.app.ActivityOptions
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import androidx.recyclerview.widget.LinearLayoutManager
import fule.com.playandroidkotlin.base.BaseFragment
import fule.com.playandroidkotlin.R
import fule.com.playandroidkotlin.ui.activity.KnowledgeActivity
import fule.com.playandroidkotlin.ui.adapter.KnowledgeAdapter
import fule.com.playandroidkotlin.ui.model.KnowledgeModel
import fule.com.playandroidkotlin.ui.presenter.KnowledgePresenter
import fule.com.playandroidkotlin.ui.util.AppConstant
import fule.com.playandroidkotlin.ui.view.KnowledgeView
import kotlinx.android.synthetic.main.fm_knowledge.*
import kotlinx.android.synthetic.main.toolbar.*
import java.util.*


/**
 * 作者： njb
 * 时间： 2018/8/7 0007-下午 5:58
 * 描述： 知识体系
 * 来源：
 */
class KnowledgeFragment : BaseFragment<KnowledgePresenter>(), KnowledgeView {
    override fun setTitle() {
        toolbar_title!!.text="知识"

    }

    private var knowledgeAdapter: KnowledgeAdapter? = null

    override fun getLayoutId(): Int {
        return R.layout.fm_knowledge
    }

    override fun loadData() {
        presenter!!.tree()
    }

    override fun initView() {
        initAdapter()

    }


    private fun initAdapter() {
        knowledgeAdapter = KnowledgeAdapter(null)
        knowledgeAdapter.run {
            val manager = LinearLayoutManager(context)
            rv_know.layoutManager = manager
/*            this!!.bindToRecyclerView(rv_know)
            this.setEmptyView(R.layout.empty_layout_no_data)*/
        }
        rv_know.run {
            rv_know.adapter = knowledgeAdapter
        }
        knowledgeAdapter!!.setOnItemClickListener { _, View, position ->
            val options: ActivityOptions = ActivityOptions.makeScaleUpAnimation(View,
                    View.width / 2,
                    View.height / 2,
                    0,
                    0)
            Intent(context, KnowledgeActivity::class.java).run {
                putExtra(AppConstant.ARTICLELIST_ID, knowledgeAdapter!!.data[position].id)
                putExtra(AppConstant.ARTICLELIST_TITLE, knowledgeAdapter!!.data[position].name)
                putParcelableArrayListExtra(AppConstant.CONTENT_DATA_KEY, knowledgeAdapter!!.data[position].children as ArrayList<out Parcelable>)
                startActivity(this,options.toBundle())
            }
        }
    }

    override fun addListener() {
    }

    override fun createPresenter(): KnowledgePresenter {
        return KnowledgePresenter(this)
    }

    override fun showLoading() {
    }

    override fun onLoadTree(data: List<KnowledgeModel.DataBean>) {
        knowledgeAdapter!!.setNewData(data)
    }

    companion object {
        fun getInstance(cid: Int): KnowledgeFragment {
            val fragment = KnowledgeFragment()
            val args = Bundle()
            args.putInt(AppConstant.ARTICLELIST_ID, cid)
            fragment.arguments = args
            return fragment
        }
    }

}


